import requests
import time

ID = "ww7b4086d88a144eec"
SECRET = "QqZ9Au1EwEDc1PyvcZaAgJBhaPDeK1z5zinENKHLL1Q"
url_get_access_token = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" + ID + "&corpsecret=" + SECRET

#获取access_token#
def get_access_token(ID,SECRET):
    access_token_json = requests.get(url_get_access_token).json()
    while access_token_json['errcode'] != 0:
        get_access_token(ID, SECRET)
        time.sleep(2)
    access_token = access_token_json['access_token']
    return access_token

#print(get_access_token(ID,SECRET))

url_get_tags_list = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_corp_tag_list?access_token=" + get_access_token(ID,SECRET)+"&debug=1"

#print(url_get_tags_list)

tags_list = requests.post(url_get_tags_list).json()['tag_group']
#print(tags_list)
for tag_group in tags_list:
    #print(tag_group)
    print(tag_group['group_id'],tag_group['group_name'])
    for tag in tag_group['tag']:
        print(tag['id'],tag['name'])


